package com.naihe.anderson.sms.widgets;

import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.naihe.anderson.sms.R;

public class AppPanel extends LinearLayout implements OnItemClickListener {
	private Context mContext;
	private List<String[]> list;
	AppAdapter adapter;

	String[][] arrays = { { "表情", String.valueOf(R.drawable.sharemore_expression_icon) },
			{ "相片", String.valueOf(R.drawable.app_panel_pic_icon) },
			{ "拍照", String.valueOf(R.drawable.app_panel_video_icon) },
			{ "位置", String.valueOf(R.drawable.app_panel_location_icon) },
			{ "电话", String.valueOf(R.drawable.app_panel_friendcard_icon) } };

	public AppPanel(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		LayoutInflater inflater = LayoutInflater.from(context);
		inflater.inflate(R.layout.app_panel, this, true);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();

		GridView app_panel_grid = (GridView) findViewById(R.id.app_panel_grid);
		app_panel_grid.setNumColumns(4);
		list = Arrays.asList(arrays);
		adapter = new AppAdapter(mContext, list);
		app_panel_grid.setAdapter(adapter);
		app_panel_grid.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		
	}

	class AppAdapter extends BaseAdapter {
		private List<String[]> alist;
		private LayoutInflater inflater;

		public AppAdapter(Context mContext, List<String[]> list) {
			this.alist = list;
			this.inflater = LayoutInflater.from(mContext);
		}

		@Override
		public int getCount() {
			return alist.size();
		}

		@Override
		public Object getItem(int position) {
			return alist.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			String[] object = (String[]) alist.get(position);
			convertView = inflater.inflate(R.layout.app_grid_item, null);
			ImageView item_icon = (ImageView) convertView.findViewById(R.id.app_grid_item_icon);
			TextView item_name = (TextView) convertView.findViewById(R.id.app_grid_item_name);
			item_icon.setBackgroundResource(Integer.parseInt(object[1]));
			item_name.setText(object[0]);
			return convertView;
		}
	}
}
